// ExtractorApi.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "HtmlPage.h"

#include <windows.h>
#include <mshtml.h>
#include <fstream>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;



typedef vector<string> strVecFilesContent;
typedef vector<string> strVecFilesNames;

int SearchDirectory(strVecFilesNames &refvecFiles,
                    const string   &refcstrRootDirectory,
                    const string   &refcstrExtension)
{
  string          strFilePath;             // Filepath
  string          strPattern;              // Pattern
  HANDLE          hFile;                   // Handle to file
  WIN32_FIND_DATA FileInformation;         // File information
	int i=0;

  strPattern = refcstrRootDirectory + "\\*." + refcstrExtension;

  hFile = ::FindFirstFile(strPattern.c_str(), &FileInformation);
  if(hFile != INVALID_HANDLE_VALUE)
  {
    do
    {
      if(FileInformation.cFileName[0] != '.')
      {
        strFilePath.erase();
        strFilePath = refcstrRootDirectory + "\\" + FileInformation.cFileName;

        if(FileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
        {
          // Search subdirectory
          int iRC = SearchDirectory(refvecFiles, strFilePath, refcstrExtension);
          if(iRC)
            return iRC;
        }
        else
        {
          // Save filename
			if ( i == 43 )
				i=i;
          refvecFiles.push_back(strFilePath);
		  i++;
        }
      }
    } while(::FindNextFile(hFile, &FileInformation) == TRUE);

    // Close handle
    ::FindClose(hFile);

    DWORD dwError = ::GetLastError();
    if(dwError != ERROR_NO_MORE_FILES)
      return dwError;
  }

  return 0;
}

void SavePagesAndKeyPhrase(string DirPath , HtmlPageVector objHPVec , KeyPhraseMap objKPMap )
{
	ofstream KPhraseFile,PagesFile;
	HtmlPageVector::iterator posHPVec;
	KeyPhraseMap::iterator posKPM;
	FeatureItemVector::iterator posFIVec;
	FeatureItemVector objFIvec;
	string KPFilePath( DirPath ), PagesFilePath( DirPath );
	PagesFilePath += "PagesMatrix.txt";
	KPFilePath += "KeyPhrases.txt";
	KPhraseFile.open( KPFilePath.c_str() , ios::out );
	PagesFile.open(PagesFilePath.c_str() , ios::out );
	KPhraseFile<<"index; "<<"KeyPhrase; "<<"Frequency;\n";
	for ( posKPM = objKPMap.begin() ; posKPM != objKPMap.end() ; posKPM++ )
		KPhraseFile << posKPM->second.GetIndex() << "; " << posKPM->first.c_str() << "; " << posKPM->second.GetFrequency()<<endl;
	KPhraseFile.close();
//	PagesFile<<objHPVec.size()<<endl;
	for ( posHPVec = objHPVec.begin() ; posHPVec != objHPVec.end() ; posHPVec++ )
	{
		objFIvec=posHPVec->GetFeatures();
	//	PagesFile<<objFIvec.size()<<" ";
		for ( posFIVec = objFIvec.begin() ; posFIVec != objFIvec.end() ; posFIVec++ )
			PagesFile<<posFIVec->GetIndex()<<" "<<posFIVec->GetWeight()<<" "<<posFIVec->GetFrequency()<<endl;
		PagesFile<<endl;
	}
	PagesFile.close();
}
string ReadFile(string FilePath)
{
	FILE *fp;
	string Stream;
	char w[2];
	w[1]='\0';
	fp=fopen( FilePath.c_str() , "r" );
	while( (fscanf(fp,"%c",&w[0])) != EOF )
		Stream.append(w,1);
	fclose(fp);
	return Stream;
}




int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0 , iRC;
	strVecFilesNames FilesNames;
	strVecFilesContent FilesContent;
	HtmlPageVector AllPagesVector;
	HtmlPage *pHtmlPage=NULL;
	char DirPath[300];
	DirPath[0]='\0';
	strVecFilesContent::iterator posFilesContent;
	strVecFilesNames::iterator posFilesNames;
	KeyPhraseCollection MyKeyPhraseCollection;
	// initialize MFC and print and error on failure
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: change error code to suit your needs
		cerr << _T("Fatal Error: MFC initialization failed") << endl;
		nRetCode = 1;
	}
	else
	{
		cout<<"please enter the web page's directory path\n";
		//cin>>DirPath;
		strcpy( DirPath , "HtmlFiles\\temp" );
	//	iRC = SearchDirectory(FilesNames, DirPath , "html");//read html files
		iRC = SearchDirectory(FilesNames, DirPath, "htm" );//read htm files
		for ( posFilesNames = FilesNames.begin() ; posFilesNames != FilesNames.end() ; posFilesNames++ )
			FilesContent.push_back( ReadFile( posFilesNames->c_str() ) );
		nRetCode=0;
		for ( posFilesContent = FilesContent.begin() ; posFilesContent != FilesContent.end() ; posFilesContent++ )
		{
			if ( nRetCode == 12 )
				nRetCode=nRetCode;
			pHtmlPage=new HtmlPage;				
			pHtmlPage->GenerateTermsList( (char*) posFilesContent->c_str() , MyKeyPhraseCollection );
			AllPagesVector.push_back( *pHtmlPage );
			nRetCode++;
			delete pHtmlPage;
			cout<<nRetCode<<" ";
		}
		cout<<"\nFinito La comd";
		cout<< "do u want to save - 1 for yes";
		cin>>nRetCode;
		nRetCode = 1;
		if ( nRetCode == 1 )
			SavePagesAndKeyPhrase("output\\" , AllPagesVector , MyKeyPhraseCollection.GetKeyPhraseMap() );
	}
	return nRetCode;
}


